
using System;
using System.Drawing;

using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.SQLite;
using bjeschool_csharp.Model;

namespace bjeschool
{
	public partial class MyClassController : UIViewController
	{
		[Outlet("navctrl")]
		public UINavigationController navctrl {get;set;}

		[Outlet("containerView")]
		public UIView containerView { get; set; }

		private UIPopoverController popfilter;
		[Action("OnPopSelect:")]
		private void OnPopSelect(UIButton sender)
		{
			if(popfilter==null)
			{
				var hwf = new HomeWorkFilter();
				popfilter = new UIPopoverController(hwf);
				popfilter.PopoverContentSize = hwf.View.Bounds.Size;
			}
			popfilter.PresentFromRect(sender.Frame, this.View, UIPopoverArrowDirection.Up, true);
		}

		[Action("OnReturn")]
		private void OnReturn ()
		{
			//navctrl.PopViewControllerAnimated(true);
			navctrl.PopToRootViewController(true);
		}

		[Action("OnNewHomeWorkClick")]
		private void OnNewHomeWorkClick ()
		{
			ShowNewHomeWork();
		}

		[Action("OnHistoryClick")]
		private void OnHistoryClick ()
		{
			ShowHistory();
		}

		private CommonListEx<M_HomeWork, HomeWorkCell> stvc;
		public void ShowHistory ()
		{
			if (stvc == null) {
				stvc = new CommonListEx<M_HomeWork, HomeWorkCell> (AppDelegate.sqlitedb);
				stvc.HeaderTitle = new string[]{"作业要求", "布置时间", "要求完成时间"};
				stvc.OnRowSelected += (UITableView tableView, NSIndexPath indexPath) => {
					if (AppDelegate.loginuser is M_Teacher) {
						HomeWorkCommitList dvc = new HomeWorkCommitList ();
						navctrl.PushViewController (dvc, true);
					} else {
						HomeWorkCommit dvc = new HomeWorkCommit ();
						navctrl.PushViewController (dvc, true);
					}
				};
				navctrl.ViewControllers = new UIViewController[]{stvc};
			}
			else
				navctrl.PopToRootViewController(true);
		}

		public void ShowNewHomeWork ()
		{
			if(stvc == null)
				ShowHistory();
			navctrl.PushViewController(new HomeWorkEdit(), true);
		}

		private HomeWorkCommit hwc;
		public void ShowCommitHomeWork ()
		{
			if(stvc == null)
				ShowHistory();
			//if(hwc==null)
				hwc = new HomeWorkCommit();
			navctrl.PushViewController(hwc, true);
		}

		public MyClassController () : base ("MyClassController", null)
		{
		}
		
		public override void DidReceiveMemoryWarning ()
		{
			// Releases the view if it doesn't have a superview.
			base.DidReceiveMemoryWarning ();
			
			// Release any cached data, images, etc that aren't in use.
		}
		
		public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();
			
			// Perform any additional setup after loading the view, typically from a nib.
			this.AddChildViewController(navctrl);
			this.containerView.AddSubview(navctrl.View);
			navctrl.View.Frame = new RectangleF(new PointF(0,0), containerView.Frame.Size);

			//navctrl.NavigationBar.RemoveFromSuperview();
			//this.containerView2.AddSubview(navctrl.NavigationBar);
			//navctrl.NavigationBar.Frame = new RectangleF(new PointF(0,0), new SizeF(300,0));

			//this.containerView2.Hidden = true;
			navctrl.NavigationBarHidden = true;

			//toolbar.Items[1].CustomView = pagebar;
			//tablectrl.TableView.Frame = new RectangleF(new PointF(0, 0), tablectrl.TableView.Frame.Size);

			ShowHistory();
		}
		
		public override void ViewDidUnload ()
		{
			base.ViewDidUnload ();
			
			// Clear any references to subviews of the main view in order to
			// allow the Garbage Collector to collect them sooner.
			//
			// e.g. myOutlet.Dispose (); myOutlet = null;
			
			ReleaseDesignerOutlets ();
		}
		
		public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
		{
			// Return true for supported orientations
			return true;
		}
	}

}

